/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.qudexapps.swfspreadsheet.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import uk.ljmu.qudexapps.swfspreadsheet.domain.Files;
import uk.ljmu.qudexapps.swfspreadsheet.services.MetadataService;
//import uk.ljmu.qudexapps.swfspreadsheet.utilities.ExcelManagement;

@Controller
public class DefaultController {
	@Autowired
	private Files zipFile;
	/**
	 * @return the zipFile
	 */
	public Files getZipFile() {
		return zipFile;
	}
	/**
	 * @param zipFile the zipFile to set
	 */
	public void setZipFile(Files zipFile) {
		this.zipFile = zipFile;
	}

	@Autowired
	private MetadataService mdService;
	
	@Autowired
	@Qualifier("mymessage")
	protected MessageSource messageSource;
	
	@RequestMapping("/intro")
	public ModelAndView intro(HttpServletRequest request)
    {		
        ModelAndView mav = new ModelAndView();
        mav.setViewName("intro");
       
        return mav;
    }
	@RequestMapping("/restart")
	public ModelAndView restart(HttpServletRequest request) {
		request.getSession().invalidate();
		
		ModelAndView mav = new ModelAndView();
        mav.setViewName("intro");
        
        return mav;
	}
	
	/**
	 * download
	 */
	@RequestMapping("/download")
	public ModelAndView download(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//boolean resultsOK;
		
		//ExcelManagement excelManager = new ExcelManagement();
		//resultsOK = excelManager.createExcelFile(response.getOutputStream(), 
		//		this.mdService.getSelectedSchemas(), 
		//		null,
		//		this.zipFile.getZipFile());
		//if 	(resultsOK) {
		//	response.setContentType( "application/vnd.ms-excel");
		//	response.setHeader("Content-Disposition","attachment; filename=\"Spreadsheet.xls\"");
		//	return null;			
		//}
		//return Errors view
		return new ModelAndView("error/generateError");

	}
	
	public void setMdService(MetadataService mdService) {
		this.mdService = mdService;
	}	
	public MetadataService getMdService() {
		return mdService;
	}
}
